package cn.zxl.spring.holiday.job.service.impl;

import cn.zxl.spring.holiday.job.domain.Permission;
import cn.zxl.spring.holiday.job.mapper.PermissionMapper;
import cn.zxl.spring.holiday.job.qo.QueryObject;
import cn.zxl.spring.holiday.job.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bofeifei
 * @since 2021-10-01
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx;

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectList(null);
        return new PageInfo<>(permissions);
    }

    @Override
    public void load() {
        List<Permission> permissionAll = permissionMapper.selectList(null);
        Set<String> permissionSet = new HashSet<>();
        for (Permission permission : permissionAll) {
            permissionSet.add(permission.getExpression());
        }
        List<Permission> permissions = new ArrayList<>();
        Map<String, Object> an = ctx.getBeansWithAnnotation(RequestMapping.class);
        System.out.println(an);
        Collection<Object> values = an.values();
        System.out.println(values);
        for (Object value : values) {
            System.out.println(value);
            // 这里获取到的value是一个动态代理类
            String cname = value.getClass().getName();
            // 我们通过 分割 获取到这个动态代理类所代理的真实类的权限类名
            String className = cname.split("\\$\\$")[0];
            try {
                // 通过权限类名反射拿到这个真实类
                Class<?> clazz = Class.forName(className);
                Method[] declaredMethods = clazz.getDeclaredMethods();
                for (Method method : declaredMethods) {
                    RequiresPermissions rp = method.getAnnotation(RequiresPermissions.class);
                    if (rp != null && !permissionSet.contains(rp.value()[1])) {
                        Permission permission = new Permission();
                        permission.setName(rp.value()[0]);
                        permission.setExpression(rp.value()[1]);
                        permissions.add(permission);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (permissions.size() > 0) {
            permissionMapper.saveList(permissions);
        }
    }

    @Override
    public List<Permission> findByRoleId(Long id) {
        return permissionMapper.findByRoleId(id);
    }

    @Override
    public List<String> findPermissionsByEid(Long id) {
        return permissionMapper.findPermissionsByEid(id);
    }


}
